package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.entity.FinancialEntity;
import com.hdjk.view.util.StringAppUtil;

import java.util.List;

/**
 * Created by sxy on 2017/11/2.
 */

public class FinancialListAdapter extends BaseAdapter {
    private List<FinancialEntity> mList;
    private Context mContext;
    private OnBuyClickListener mOnBuyClick;

    public FinancialListAdapter(Context context,OnBuyClickListener onBuyClick) {
        mContext = context;
        mOnBuyClick=onBuyClick;
    }

    public void setDataList(List<FinancialEntity> list){
        mList=list;
    }
    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public FinancialEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null){
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_financial_list, parent,false);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            holder.tv_earnings = (TextView) convertView.findViewById(R.id.tv_earnings);
            holder.btn_buy = (TextView) convertView.findViewById(R.id.btn_buy);
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        final FinancialEntity entity=mList.get(position);
        holder.tv_name.setText(entity.getSubShortName());
        holder.tv_number.setText(entity.getProductCode());
        holder.tv_type.setText(entity.getRevenueMode());
        if (!StringAppUtil.isEmptyOrNull(entity.getMinInitialBuyAmount())) {
            holder.tv_money.setText(entity.getMinInitialBuyAmount() + "元");
        }
        if (!StringAppUtil.isEmptyOrNull(entity.getNav())) {
            holder.tv_earnings.setText(entity.getNav());
        }
        if (entity.getIsBuyEnable().equals("Y")){
            holder.btn_buy.setText("立即购买");
            holder.btn_buy.setBackgroundResource(R.drawable.yellow_de9e6b_button_selector);
            holder.btn_buy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnBuyClick.onBuyClick(entity);
                }
            });
        }else {
            holder.btn_buy.setText("已售罄");
            holder.btn_buy.setBackgroundResource(R.color.black_999999);
        }
        return convertView;
    }

    class  Holder{
         TextView tv_name;
         TextView tv_number;
         TextView tv_type;
         TextView tv_money;
         TextView tv_earnings;
         TextView btn_buy;

    }

    public interface OnBuyClickListener{
        public void onBuyClick(FinancialEntity financialEntity);
    }
}
